package com.sinven.controller;


import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinven.pojo.Message;
import com.sinven.pojo.User;
import com.sinven.service.ArticleService;
import com.sinven.service.MessageService;
import com.sinven.service.UserService;

/**
 * 留言
 * @author 
 *
 */
@Controller
public class leacotsController {
	@Autowired
	private MessageService messageServiceImpl;
	@Autowired
	private UserService userServiceImpl;
	@Autowired
	private ArticleService articleServiceImpl;
	
	@RequestMapping("leacots" )
	public String article( HttpSession session,Model model) {
		
			User user = (User)session.getAttribute("user");
			System.out.println(user);
			try {
				List<Message> selectMessageByOther = messageServiceImpl.SelectMessageByOther(user.getU_id());
				model.addAttribute("selectMessageByOther", selectMessageByOther);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		
		
		return "pages/leacots";
	}
	@RequestMapping("/message")
	public String Message(Integer u_id,Model model) {
		try {
			User selectUserById = userServiceImpl.SelectUserById(u_id);
			model.addAttribute("selectUserById", selectUserById);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		model.addAttribute("artId",u_id );
		 
		return "pages/message";
	}
	@RequestMapping("con")
	@ResponseBody
	public boolean addMessage(Message message, HttpSession session ) throws Exception {
		User user = (User)session.getAttribute("user");
		System.out.println(user);
		message.setM_fromuserId(user); 
		System.out.println(message);
		boolean res = messageServiceImpl.AddMessage(message);
		if(res==true) {
			return res;
		}
		return res;

	}
	
}
